package com.example.androidparser;



import android.app.Activity;
import android.os.Bundle;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
//import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class Android_withParser extends Activity {	

	private StatusUpdateReceiver statusUpdateIntentReceiver;
	private MQTTMessageReceiver  messageIntentReceiver;
	@Override

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        statusUpdateIntentReceiver = new StatusUpdateReceiver();
	    IntentFilter intentSFilter = new IntentFilter( AndroidParser_service.MQTT_STATUS_INTENT);
	    registerReceiver(statusUpdateIntentReceiver, intentSFilter);  

	    messageIntentReceiver = new MQTTMessageReceiver();
	    IntentFilter intentCFilter = new IntentFilter( AndroidParser_service.MQTT_MSG_RECEIVED_INTENT);
	    registerReceiver(messageIntentReceiver, intentCFilter);  

        
        
        
        final Button Broker = ((Button) findViewById(R.id.broker_button));  

        
  	  	
  	  Broker.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {	
				Log.d("ClientActivity", "C:in activity...");
				AndroidParser_service.actionStart(getApplicationContext());
		    /*	Editor editor = getSharedPreferences(PushService.TAG, MODE_PRIVATE).edit();
		    	editor.putString(PushService.PREF_DEVICE_ID, mDeviceID);
		    	editor.commit();
				PushService.actionStart(getApplicationContext());		        
		  		startButton.setEnabled(false);
		  		stopButton.setEnabled(true);*/				
			}
		});
  	
	}
	
	
	public class StatusUpdateReceiver extends BroadcastReceiver
	{
	    @Override
	    public void onReceive(Context context, Intent intent)
	    {
	    	Log.d("inside client acticity","Inside status receive");
	        Bundle notificationData = intent.getExtras();
	        String newStatus = notificationData.getString( AndroidParser_service.MQTT_STATUS_MSG);	    	  
	        ((TextView) findViewById(R.id.messageboxtxt)).setText(newStatus);
	        
	    }
	}
	public class MQTTMessageReceiver extends BroadcastReceiver
	{
	    @Override
	    public void onReceive(Context context, Intent intent)
	    {
	    	Log.d("inside client acticity","Inside message receive");
	        Bundle notificationData = intent.getExtras();
	        String newTopic = notificationData.getString( AndroidParser_service.MQTT_MSG_RECEIVED_TOPIC);
	        String newData  = notificationData.getString( AndroidParser_service.MQTT_MSG_RECEIVED_MSG);	    	  
	        ((TextView) findViewById(R.id.messageboxtxt)).setText(newData);
	        
	    }
	}  
	@Override
	protected void onDestroy()
	{
	    

	    unregisterReceiver(statusUpdateIntentReceiver);
	    unregisterReceiver(messageIntentReceiver);  

	    
	} 
}